package com.zbkj.crmeb.merchant.admin.controller;

import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.enums.ProductTypeEnum;
import com.zbkj.crmeb.merchant.admin.service.MerAgentStoreProductService;
import com.zbkj.crmeb.store.request.StoreProductSearchRequest;
import com.zbkj.crmeb.store.response.StoreProductNormResponse;
import com.zbkj.crmeb.store.response.StoreProductResponse;
import com.zbkj.crmeb.store.response.StoreProductTabsHeader;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * 服务商-商品管理
 *
 * @author lvmd
 * @date 2022-02-18
 */
@RestController
@RequestMapping("api/admin/mer/agent/store/product")
@Api(tags = "服务商-商品管理")
public class MerAgentStoreProductController extends AdminBaseController {

    @Autowired
    private MerAgentStoreProductService agentStoreProductService;

    /**
     * 服务商自建商品列表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "自建商品分页列表")
    @GetMapping(value = "/inside/list")
    public CommonResult<CommonPage<StoreProductResponse>> listInsideStoreProducts(@Validated StoreProductSearchRequest request,
                                                                                  @Validated PageParamRequest pageParamRequest) {
        request.setIsEurope(ProductTypeEnum.INSIDE.getCode());
        PageInfo<StoreProductResponse> pageInfo = agentStoreProductService.listStoreProducts(request, pageParamRequest);
        if (pageInfo == null) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * 服务商云仓商品列表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "云仓商品分页列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<StoreProductResponse>> listStoreProducts(StoreProductSearchRequest request,
                                                                            @Validated PageParamRequest pageParamRequest) {
        List<Integer> isNotEurope = Collections.singletonList(ProductTypeEnum.INSIDE.getCode());
        request.setIsNotEurope(isNotEurope);
        PageInfo<StoreProductResponse> pageInfo = agentStoreProductService.listStoreProducts(request, pageParamRequest);
        if (pageInfo == null) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }


    /**
     * 服务商云仓商品详情
     *
     * @param id Integer
     */
    @ApiOperation(value = "云仓商品详情")
    @GetMapping(value = "/info/{id}")
    public CommonResult<StoreProductNormResponse> info(@PathVariable Integer id, HttpServletRequest request) {
        StoreProductNormResponse storeProductResponse = agentStoreProductService.getSupplierByProductId(id, getMerIdByNull(request));
        return CommonResult.success(storeProductResponse);
    }

    /**
     * 服务商自建商品详情
     *
     * @param id Integer
     */
    @ApiOperation(value = "自建商品详情")
    @GetMapping(value = "/inside/info/{id}")
    public CommonResult<StoreProductResponse> insideInfo(@PathVariable Integer id, HttpServletRequest request) {
        StoreProductResponse storeProductResponse = agentStoreProductService.getByProductId(id, getMerIdByNull(request));
        return CommonResult.success(storeProductResponse);
    }


    /**
     * 商品tabs表头数据
     *
     * @return
     */
    @ApiOperation(value = "供应商商品表头数量")
    @GetMapping(value = "/tabs/headers")
    public CommonResult<List<StoreProductTabsHeader>> getTabsHeader() {
        return CommonResult.success(agentStoreProductService.getTabsHeader(2));
    }

    /**
     * 商品tabs表头数据
     *
     * @return
     */
    @ApiOperation(value = "自建商品表头数量")
    @GetMapping(value = "/inside/tabs/headers")
    public CommonResult<List<StoreProductTabsHeader>> getInsideTabsHeader() {
        return CommonResult.success(agentStoreProductService.getTabsHeader(1));
    }


}
